package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.*;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class SysAdminPresenter implements ActionListener{
	private DataBaseAccessor dba;
	private SysAdmin sysAdmin;
	private ISysAdminView myView;
	
	public SysAdminPresenter(SysAdmin sysAdmin, ISysAdminView view){
		this.sysAdmin = sysAdmin;
		this.myView = view;
		this.myView.addListener(this);
		this.myView.showSelf();
		
	}
	
	public SysAdminPresenter(SysAdmin sysAdmin2, DataBaseAccessor dba2) {
		this.sysAdmin = sysAdmin2;
		this.dba = dba2;
		this.myView = new SysAdminWindow();
		this.myView.addListener(this);
		this.myView.showSelf();
		this.dba.connectToDatabase();
	}

	public void actionPerformed(ActionEvent event) {
		User user= null;
		if ( event.getSource() == myView.getExitButton()){
//			sysAdmin.setLoggedIn(false);
//
//			dba= new DataBaseAccessor();
//			dba.connectToDatabase();
//			
//			dba.saveUser(sysAdmin);

			System.exit(0);
		}
		
		else if( event.getSource() == myView.getCreateUserButton()){
			
			Object[] options = {"Patient", "Doctor", "Nurse", "SysAdmin", "Cancel"};
			int option= JOptionPane.showOptionDialog(null,"What kind of user would you like to create?", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel

			if(option == 0)
			{
				UserInfoPresenter pres= new UserInfoPresenter(0, dba);
			}
			else if(option == 1)
			{
				UserInfoPresenter pres= new UserInfoPresenter(1, dba);
			}
			else if(option == 2)
			{
				UserInfoPresenter pres= new UserInfoPresenter(2, dba);
			}
			else if(option==3)
			{
				UserInfoPresenter pres= new UserInfoPresenter(3, dba);

			}
		}
		
		else if( event.getSource() == myView.getDeleteUserButton()){
			dba= new DataBaseAccessor();
			dba.connectToDatabase();

			
			Object[] options = {"Patient", "Doctor", "Nurse", "SysAdmin", "Cancel"};
			int option= JOptionPane.showOptionDialog(null,"What kind of user would you like to delete?", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel

			if(option == 0)
			{
				
				ArrayList<Patient> patientList= dba.getAllPatients();
				Patient[] patientArr= new Patient[patientList.size()];
				for(int i = 0; i<patientArr.length; i++){
					patientArr[i]= patientList.get(i);
				}
				
				String[] pNamesandEmails= new String[patientArr.length];
				for(int i = 0; i<patientArr.length; i++){
					pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
				}
				
				String chosen= (String)JOptionPane.showInputDialog(null, "Choose a patient.", "Delete Patient", JOptionPane.QUESTION_MESSAGE, null, pNamesandEmails, pNamesandEmails[0]);
				if(chosen != null){
					int index= this.getChosenIndex(pNamesandEmails, chosen);
					System.out.println(index);
					
					Patient p= patientList.get(index);
					
					System.out.println(p.getEmail());
					
					dba.removeUser(p);
				}
				
			}
			else if(option == 1)
			{
				
				ArrayList<Doctor> doctorList= dba.getAllDoctors();
				Doctor[] doctorArr= new Doctor[doctorList.size()];
				for(int i = 0; i<doctorArr.length; i++){
					doctorArr[i]= doctorList.get(i);
				}
				
				String[] dNamesandEmails= new String[doctorArr.length];
				for(int i = 0; i<doctorArr.length; i++){
					dNamesandEmails[i]= doctorArr[i].getName().toString() + " - " + doctorArr[i].getEmail();
				}
				
				String chosen= (String)JOptionPane.showInputDialog(null, "Choose a doctor.", "Delete Doctor", JOptionPane.QUESTION_MESSAGE, null, dNamesandEmails, dNamesandEmails[0]);
				if(chosen != null){
					int index= this.getChosenIndex(dNamesandEmails, chosen);
					System.out.println(index);
					
					Doctor d= doctorList.get(index);
					
					dba.removeUser(d);
				}
				
				
			}
			else if(option == 2)
			{
				
				ArrayList<Nurse> nurseList= dba.getAllNurses();
				Nurse[] nurseArr= new Nurse[nurseList.size()];
				for(int i = 0; i<nurseArr.length; i++){
					nurseArr[i]= nurseList.get(i);
				}
				
				String[] nNamesandEmails= new String[nurseArr.length];
				for(int i = 0; i<nurseArr.length; i++){
					nNamesandEmails[i]= nurseArr[i].getName().toString() + " - " + nurseArr[i].getEmail();
				}
				
				String chosen= (String)JOptionPane.showInputDialog(null, "Choose a nurse.", "Delete Nurse", JOptionPane.QUESTION_MESSAGE, null, nNamesandEmails, nNamesandEmails[0]);
				if(chosen != null){
					int index= this.getChosenIndex(nNamesandEmails, chosen);
					System.out.println(index);
					
					Nurse n= nurseList.get(index);
					
					dba.removeUser(n);
				}
				
				
			}
			else if(option==3)
			{
				
				ArrayList<SysAdmin> sysAdminList= dba.getAllSysAdmins();
				SysAdmin[] sysAdminArr= new SysAdmin[sysAdminList.size()];
				for(int i = 0; i<sysAdminArr.length; i++){
					sysAdminArr[i]= sysAdminList.get(i);
				}
				
				String[] saNamesandEmails= new String[sysAdminArr.length];
				for(int i = 0; i<sysAdminArr.length; i++){
					saNamesandEmails[i]= sysAdminArr[i].getName().toString() + " - " + sysAdminArr[i].getEmail();
				}
				
				String chosen= (String)JOptionPane.showInputDialog(null, "Choose a System Administrator.", "Delete System Administrator", JOptionPane.QUESTION_MESSAGE, null, saNamesandEmails, saNamesandEmails[0]);
				if(chosen != null){
					int index= this.getChosenIndex(saNamesandEmails, chosen);
					System.out.println(index);
					
					SysAdmin sa= sysAdminList.get(index);
					
					dba.removeUser(sa);
				}

			}
			else{
				//do nothing. the sys admin canceled the task
			}
		}
		
		else if( event.getSource() == myView.getEditUserButton()){
			Object[] options = {"Patient", "Doctor", "Nurse", "SysAdmin", "Cancel"};
			int option= JOptionPane.showOptionDialog(null,"What kind of user would you like to edit?", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); //0-patient, 1-doctor, 2-nurse, 3-doctor, 4-cancel

			if(option == 0)
			{
				EditUserInfoPresenter pres= new EditUserInfoPresenter(0, new PatientInformationWindow());
			}
			else if(option == 1)
			{
				EditUserInfoPresenter pres= new EditUserInfoPresenter(1, new UserInformationWindow());
			}
			else if(option == 2)
			{
				EditUserInfoPresenter pres= new EditUserInfoPresenter(2, new UserInformationWindow());
			}
			else if(option==3)
			{
				EditUserInfoPresenter pres= new EditUserInfoPresenter(3, new UserInformationWindow());

			}
			else{
				//do nothing. the sys admin canceled the task
			}
		}
		
		else if( event.getSource() == myView.getViewUsersButton()){
			String[] columnNames={"Name", "id", "email", "position"};
			ArrayList<User> users= dba.getAllUsers();
			int rows= users.size();
			String[][] info= new String[rows][4];
			for(int a=0; a<rows; a++)
			{
				info[a][0]= users.get(a).getName().toString();
				info[a][1]= Long.toString(users.get(a).getId());
				info[a][2]= users.get(a).getEmail();
				info[a][3]= users.get(a).getType().toString();
			}
			
			ViewUsersPresenter view= new ViewUsersPresenter(info, columnNames);
		}
		
		else if( event.getSource() == myView.getSearchPatientButton()){
			FindByNamePresenter	pres= new FindByNamePresenter(new NameWindow());
					
		}
		
		else if( event.getSource() == myView.getTreatmentButton()){
			String[] columnNames={"Name", "id", "email", "position"};
			ArrayList<User> users= dba.getAllUsers();
			int rows= users.size();
			String[][] info= new String[rows][4];
			for(int a=0; a<rows; a++)
			{
				info[a][0]= users.get(a).getName().toString();
				info[a][1]= Long.toString(users.get(a).getId());
				info[a][2]= users.get(a).getEmail();
				info[a][3]= users.get(a).getType().toString();
			}
			
			ViewUsersPresenter view= new ViewUsersPresenter(info, columnNames);
		}
		
		else if( event.getSource() == myView.getApptPageButton()){
			Object[] options = {"View", "Edit", "Delete", "Cancel"};
			int option= JOptionPane.showOptionDialog(null,"What would you like to do to the appointment?", "Dr. Doom Management", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); //0-view, 1-edit, 2-delete, 3-cancel

			if(option == 0)
			{
				ViewAppointmentPresenter pres= new ViewAppointmentPresenter(0, null, new ViewDailyAppointmentWindow());
			}
			else if(option == 1)
			{
				ViewAppointmentPresenter pres= new ViewAppointmentPresenter(1, null, new ViewDailyAppointmentWindow());
			}
			else if(option == 2)
			{
				ViewAppointmentPresenter pres= new ViewAppointmentPresenter(2, null, new ViewDailyAppointmentWindow());
			}
			else{
				//do nothing. the sys admin canceled the task
			}
		}

		else if( event.getSource() == myView.getReportButton()){
			System.out.println("Preports window");
			ArrayList<Patient> patientList= dba.getAllPatients();
			Patient[] patientArr= new Patient[patientList.size()];
			for(int i = 0; i<patientArr.length; i++){
				patientArr[i]= patientList.get(i);
			}
			
			String[] pNamesandEmails= new String[patientArr.length];
			for(int i = 0; i<patientArr.length; i++){
				pNamesandEmails[i]= patientArr[i].getName().toString() + " - " + patientArr[i].getEmail();
			}
			
			String chosen= (String)JOptionPane.showInputDialog(null, "Choose a patient.", "Delete Patient", JOptionPane.QUESTION_MESSAGE, null, pNamesandEmails, pNamesandEmails[0]);
			if(chosen != null){
				int index= this.getChosenIndex(pNamesandEmails, chosen);
				System.out.println(index);
				
				Patient p= patientList.get(index);
				
				System.out.println(p.getEmail());
				
				PatientHealthHistoryPresenter presRec = new PatientHealthHistoryPresenter(p, dba );

			}
		}
		
		else if( event.getSource() == myView.getSetPasswordButton()){
			String emailAttempt= JOptionPane.showInputDialog("Enter User's Email Address.");
			if ( emailAttempt == null )
				return;
			user= dba.findByEmail(emailAttempt);
			if(user == null)
			{
				JOptionPane.showMessageDialog(null, "User not found in the system.");
			}
			else{
				user.setPassword(JOptionPane.showInputDialog("Enter desired password"));
				dba.saveUser(user);
			}
		}
		
		else if( event.getSource() == myView.getSuspendButton()){
			String emailAttempt= JOptionPane.showInputDialog("Enter User's Email Address.");

			user= dba.findByEmail(emailAttempt);
			if(user == null)
			{
				JOptionPane.showMessageDialog(null, "User not found in the system.");
			}
			else{
				Object[] options = {"Suspend", "Unsupend", "Cancel"};
				int option= JOptionPane.showOptionDialog(null,"What would you like to do?", "Suspend/Unsuspend", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); //0-suspend, 1-unsuspend, 2-cancel

				if(option==0)
				{
					user.setSuspended(true);
				}
				else if(option==1)
				{
					user.setSuspended(false);
				}
				else{
					//do nothing
				}
				dba.saveUser(user);
			}	
		}
	}
	
	
	private int getChosenIndex(String[] arr, String target){
		int i=0;
		boolean found= false;
		
		while(!found){
			if(arr[i].equals(target)){
				found= true;
			}
			else{
				i++;
			}
		}
		return i;
	}
}
